package com.pay.util;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.pay.common.DiskNameEnum;

public class OssImgUtil {

	/**
	 * 上传图片到Oss服务器
	 * @param file 文件流
	 * @param diskId 1:活动图片,2:会员卡,3:团建图片,4:作品图,5:场馆,6:课程,7:会员图片
	 * @return 图片地址
	 */
	public static String UploadImgOss(File file,Integer diskId){
		String imgUrl = "";
		try {
			String diskName = DiskNameEnum.getStateName(diskId);
			OSSClient client = OSSUnit.getOSSClient(); 
			imgUrl = OSSUnit.uploadObject2OSS(client, file, diskName);
			if (!StringTools.isEmpty(imgUrl)) {
				imgUrl = imgUrl.substring(0, imgUrl.indexOf("?")); 
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return imgUrl;
	}
	
	/**
	 * 上传图片到Oss服务器
	 * @param is 文件流
	 * @param diskId 1:活动图片,2:会员卡,3:团建图片,4:作品图,5:场馆,6:课程,7:会员图片
	 * @return 图片地址
	 */
	public static String UploadImgOss(InputStream is,Integer diskId){
		String imgUrl = "";
		try {
			String diskName = DiskNameEnum.getStateName(diskId);
			OSSClient client = OSSUnit.getOSSClient(); 
			imgUrl = OSSUnit.uploadObject2OSS(client, is, diskName);
			if (!StringTools.isEmpty(imgUrl)) {
				imgUrl = imgUrl.substring(0, imgUrl.indexOf("?")); 
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return imgUrl;
	}
	
	/**
	 * 上传图片到Oss服务器,返回url
	 * @param file 文件流
	 * @param diskId 1:活动图片,2:会员卡,3:团建图片,4:作品图,5:场馆,6:课程,7:会员图片
	 * @return
	 */
	public static String UploadImgOss(MultipartFile file,Integer diskId){
		String imgUrl = "";
		try {
			String diskName = DiskNameEnum.getStateName(diskId);
			OSSClient client = OSSUnit.getOSSClient(); 
			imgUrl = OSSUnit.uploadObject2OSS(client, file, diskName);
			if (!StringTools.isEmpty(imgUrl)) {
				imgUrl = imgUrl.substring(0, imgUrl.indexOf("?")); 
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return imgUrl;
	}
	
	/**
	 * 根据文件名称删除
	 * @param diskName 文件夹名称
	 * @param imgName 图片名称
	 * @return
	 */
	public static boolean deleteImgOss(String diskName,String imgName){
		try {
			OSSClient client = OSSUnit.getOSSClient(); 
			OSSUnit.deleteFile(client, diskName, imgName);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	 /**
     * @Description: 根据key获取oss服务器上的图片地址
     * @param key
     * @return 
     * @ReturnType:String
    */
    public static String getImgURl(String key){
    	OSSClient client = OSSUnit.getOSSClient(); 
        Date expires = new Date (new Date().getTime()+(3000*60)); // 30 minute to expire
        GeneratePresignedUrlRequest generatePresignedUrlRequest ;
        if(key.startsWith("msplatform")){
            generatePresignedUrlRequest =new GeneratePresignedUrlRequest("meibaxiu", key);
        }else {
            generatePresignedUrlRequest =new GeneratePresignedUrlRequest("meibaxiu", key);
        }
        generatePresignedUrlRequest.setExpiration(expires);
        URL url = client.generatePresignedUrl(generatePresignedUrlRequest);
        return url.toString();
    }
}
